//
//  SuggestedCompanionsTableViewController.m
//  PlotMate
//
//  Created by Brian Boyle on 19/04/2012.
//  Copyright (c) 2012 Brian Boyle. All rights reserved.
//

#import "SuggestedCompanionsTableViewController.h"
#import "Plant.h"
#import "UILabel+TextFont.h"

@interface SuggestedCompanionsTableViewController()
@property (nonatomic, strong) NSArray *potentialCompanions;
@property (nonatomic, strong) NSArray *selectedCompanions;
@end

@implementation SuggestedCompanionsTableViewController
@synthesize plants = _plants;
@synthesize potentialCompanions = _potentialCompanions;
@synthesize delegate = _delegate;
@synthesize selectedCompanions = _selectedCompanions;


#pragma mark - View lifecycle

-(void)loadSuggestedCompanions
{
    NSMutableSet *set = [[NSMutableSet alloc] init];
    //Iterate over all the plants in a bed and combine each of their companions.
    //We are using an NSSet so the list will only contain unique plants.
    for (Plant *plant in self.plants) {
        [set unionSet:plant.companions];
    }
    self.potentialCompanions = [NSArray arrayWithArray:[set allObjects]];
    NSLog(@"Size : %d", [self.potentialCompanions count]);
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    //Make the table into a multi select table that allows the user to select more than one row at a time
    self.tableView.editing = YES;
    self.tableView.allowsMultipleSelectionDuringEditing = YES;
    
    //Set the background image
    UIImageView *tempImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"paper.png"]];
    [tempImageView setFrame:self.tableView.frame]; 
    self.tableView.backgroundView = tempImageView;
    
    [self loadSuggestedCompanions];
    
}


- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.potentialCompanions count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"companionCell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    
    Plant *companionPlant = [self.potentialCompanions objectAtIndex:indexPath.row];
    UIImageView *imageView = (UIImageView *)[cell viewWithTag:1];
    imageView.image = [UIImage imageNamed:[companionPlant.name stringByAppendingString:@"_S"]];
    
    UILabel *plantLabel = (UILabel *)[cell viewWithTag:2];
    plantLabel.text = companionPlant.name;
    [plantLabel setSketchBlockFont];
    
    //Make the background of the cell to eb clean and then dont show any gb image or colour when the user selects that cell.
    [cell setBackgroundColor:[UIColor clearColor]];
    [cell setBackgroundView:[[UIView alloc] initWithFrame:CGRectZero]];
    UIView *bgView = [[UIView alloc] init];
    bgView.frame = cell.frame;
    [bgView setBackgroundColor:[UIColor clearColor]];
    [cell setSelectedBackgroundView:bgView];
    return cell;
}

/*Custom view for the hearder*/
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UILabel *header = [[UILabel alloc] init];
    header.frame = CGRectMake(72, 10, 284, 23);
    header.text = @"Select companion plants to add";
    [header setMVBoliFont];
    header.backgroundColor = [UIColor clearColor];
    
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(135, 0, 320, 10)];
    [view addSubview:header];
    return view;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 45;
}

#pragma mark - Table view delegate

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    self.selectedCompanions = [tableView indexPathsForSelectedRows];
}

- (IBAction)cancelButtonPressed:(UIBarButtonItem *)sender 
{
    [self.presentingViewController dismissModalViewControllerAnimated:YES];
}

/*User taps add button to add the selected companions to a bed. We call the suggestedCompanionsPlantSelected delegate to handle this action.*/
- (IBAction)addButtonPressed:(UIBarButtonItem *)sender 
{
    NSMutableArray *companionsToAdd = [[NSMutableArray alloc] init];
    for (NSIndexPath *indexPath in self.selectedCompanions) {
        Plant *plant = [self.potentialCompanions objectAtIndex:indexPath.row];
       
        [companionsToAdd addObject:plant];
    }       
    [self.delegate suggestedCompanionPlantsSelected:companionsToAdd];
    [self.presentingViewController dismissModalViewControllerAnimated:YES];
}
@end
